package com.itheima.wemedia.controller;


import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.core.controller.AbstractCoreController;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.WmNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description <p>自媒体文章信息</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.controller
 */
@Api(value="WmNewsController",tags = "自媒体文章内容信息")
@RestController
@RequestMapping("/news")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService=wmNewsService;
    }

    /**
     * 分页条件查询
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/list")
    @ApiOperation("分页查询")
    public PageResultVo<WmNews> findPage(@RequestBody WmNewsPageReqDto dto) {
        PageResultVo<WmNews> pageResult = wmNewsService.findPage(dto);
        return pageResult;
    }
    /**
     * 发布文章
     * @param draft
     * @param dto
     * @return
     */
    @PostMapping("/submit")
    public ResultVo<Integer> submit(Boolean draft, @RequestBody WmNewsDtoSave dto){
        System.out.println("draft = " + draft);
        Integer wmNewsId = wmNewsService.submit(draft, dto);
        return ResultVo.ok(wmNewsId);
    }


    /**
     * 根据id查询
     * @param newsId
     * @return
     */
    @GetMapping("/one/{newsId}")
    public ResultVo<WmNews> findOneById(@PathVariable(value = "newsId") int newsId){
        WmNews news = wmNewsService.getById(newsId);
        return ResultVo.ok(news);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @GetMapping("/del_news/{id}")
    public ResultVo<WmNews> deleteNews(@PathVariable(name = "id") Integer id){
        wmNewsService.deleteNews(id);
        return ResultVo.ok();
    }

    /**
     * 文章上架
     * @param map
     * @return
     */
    @PostMapping("/down_or_up")
    public ResultVo<WmNews> grounding(@RequestBody Map<String,Integer> map){
        wmNewsService.down(map);
        return ResultVo.ok();
    }
    
}

